package com.yaowk.authc.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yaowk.authc.model.base.BaseUser;
import com.yaowk.authc.model.page.PageOne;

import java.util.List;

/**
 * 用户
 * Created by yaowk on 2017/3/31.
 */
public class User extends BaseUser<User> {
    public static User dao = new User();

    /**
     * 通过用户名查询用户信息
     *
     * @param username
     * @return
     */
    public User findByUserName(String username) {
        return findFirst(getSql("user.selectByUsername"), username);
    }

    /**
     * 通过平台id和类型查询用户
     *
     * @param platformId
     * @param type
     * @return
     */
    public List<User> findByPlatformIdAndType(Integer platformId, Integer type) {
        Record record = new Record().set("platformId", platformId).set("type", type).removeNullValueColumns();
        return find(getSql("user.selectByPlatformIdAndType", record.getColumns()), record.getColumnValues());
    }

    /**
     * 统计用户数量
     *
     * @param platformId
     * @return
     */
    public Long count(Integer platformId) {
        Record record = new Record().set("platformId", platformId).removeNullValueColumns();
        return Db.queryLong(getSql("user.count", record.getColumns()), record.getColumnValues());
    }

    /**
     * 查看用户列表
     *
     * @param pageOne
     * @return
     */
    public Page<User> paginate(PageOne pageOne) {
        Record record = pageOne.toRecord();
        return paginate(pageOne.getPageNumber(), pageOne.getPageSize(), getSql("user.select"), getSql("user.sqlExceptSelect", record.getColumns()), record.getColumnValues());
    }
}
